var account,account1,ok1, ok2, ok3 = false, ok4, ok5 = false, ok6, ok7, ok8;
var send, send1;
$('#forget_content>div>span').click(function () {
    $('input').val("");
    $('#telform b').text("");
    $('#emailform b').text("");
    $(this).addClass('click').siblings().removeClass('click');
    $('#forget_content>ul').hide();
    $('#forget_content>form>ul').hide();
    var str = $(this).text();
    if (str == "手机号") {
        $('#forget_content>form>ul').eq(0).show();
    } else if (str == "人工") {

        $('#forget_content>ul').eq(0).show();
    } else {
        $('#forget_content>form>ul').eq(1).show();
    }
    ;
});
//账号
$('#account').blur(function () {
    var text = $(this).val();
     var pattern = /^\S{6,20}$/;
    if (pattern.test(text)) {
        $.get("verification.jsp?test=account&account=" + $(this).val(), function (data) {
            if (data == 1) {
                account = true;
                $('#account').next().text("√ 用户名正确").css("color", "#5ce078");
            } else {
                account = false;
                $('#account').next().text("！ 用户未注册").css("color", "#fe8d74");
            }
        });
    }
});
//账号1
$('#account1').blur(function () {
    var text = $(this).val();
     var pattern = /^\S{6,20}$/;
    if (pattern.test(text)) {
        $.get("verification.jsp?test=account&account=" + $(this).val(), function (data) {
            if (data == 1) {
                account1 = true;
                $('#account1').next().text("√ 用户名正确").css("color", "#5ce078");
            } else {
                account1 = false;
               $('#account1').next().text("！ 用户未注册").css("color", "#fe8d74");
            }
        });
    }
});
$('input[name=telphone]').blur(function () {
//手机号码找回密码
    var text = $(this).val();
    var pattern = /^1[34578]\d{9}$/;
    if (pattern.test(text)) {
        $.get("verification.jsp?test=forget&telphone=" + $(this).val(), function (data) {
            if (data == 1) {
                ok1 = true;
                send = 1;
                $('ul>li>p.verification').css("background", "#1e9fff");
                $('input[name=telphone]').next().text("√ 手机号正确").css("color", "#5ce078");
            } else {
                ok1 = false;
                send = 0;
                $('ul>li>p.verification').css("background", "#cbcbcb");
                $('input[name=telphone]').next().text("！ 手机号未注册").css("color", "#fe8d74");
            }
        });
    }
});
//发送短信
$('ul>li>p.verification').click(function () {
    var phone = $('input[name=telphone]').val();
    var i = 60;
    if (send == 1) {
        send = 0;
        $('ul>li>p.verification').css("background", "#cbcbcb");
        new Ajax("seccode.jsp").keyvalue("phone", phone).post(function (data) {
            var iCount = setInterval(function () {
                i--;
                $('ul>li>p.verification').text("(" + i + ")重新发送");
                if (i == 0) {
                    send = 1;
                    $('ul>li>p.verification').text("获取短信验证码");
                    $('ul>li>p.verification').css("background", "#1e9fff");
                    clearInterval(iCount);
                }
            }, 1000);
        });
    }
});

//短信验证码
$('#note').blur(function () {
    var key = $(this).val();
    if (MD5(key) == Cookie.Get("key")) {
        $('#note').next().next().text("√ 验证码输入正确").css("color", "#5ce078");
        ok7 = true;
    } else {
        ok7 = false;
        $('#note').next().next().text("！验证码输入有误").css("color", "#fe8d74");
    }

});
//密码
$('input[name=pwd]').blur(function () {
    var text = $(this).val();
    var pattern = /^\w{6,20}$/;
    if (!pattern.test(text)) {
        $(this).next().text("！6-20字符以内").css("color", "#fe8d74");
        ok2 = false;
    } else {
        $(this).next().text("√ 输入密码有效").css("color", "#5ce078");
        ok2 = true;
    }
});
//确认密码
$('#again').blur(function () {
    if (ok2) {
        if ($('input[name=pwd]').val() == $(this).val()) {
            ok3 = true;
            $(this).next().text("！密码一致").css("color", "#5ce078");
        } else {
            ok3 = false;
            $(this).next().text("！两次密码不一致").css("color", "#fe8d74");
        }
    }
});
//验证验证码

var validate = new Validated();
$('button').eq(0).click(function () {
    if (ok1 && ok2 && ok3 && ok7 == true) {
        alert($('input[name=note]').val());
        new Ajax("/login/update.jsp").keyvalue($("#telform").serializeArray()).getJson(function (json) {
            if (json == 1) {
                document.location = "login.jsp";
            } else {
                alert("输入的信息有误");
            }
        });
    }
});
//邮箱找回密码
$('input[name=email]').blur(function () {
    var text = $(this).val();
    var pattern = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    if (pattern.test(text)) {
        $.get("verification.jsp?test=email&email=" + $(this).val(), function (data) {
            if (data == 1) {
                send1 = 1;
                ok4 = true;
                $('ul>li>p.verification').css("background", "#1e9fff");
                $('input[name=email]').next().text("√ 邮箱正确").css("color", "#5ce078");
            } else {
                alert();
                ok4 = false;
                send1 = 0;
                $('ul>li>p.verification').css("background", "#cbcbcb");
                $('input[name=email]').next().text("邮箱未注册！").css("color", "#fe8d74");
            }
        });
    } else {
        $('input[name=email]').next().text("请输入正确的邮箱账号！").css("color", "#fe8d74");
    }

});
//通过邮箱发送验证码
$('ul>li>p.verification').click(function () {
    var email = $('input[name=email]').val();
    var i = 60;
    if (send1 == 1) {
        $('ul>li>p.verification').css("background", "#cbcbcb");
        $.get("secemail.jsp?email=" + email);
        send1 = 0;
        var iCount = setInterval(function () {
            i--;
            $('ul>li>p.verification').text("(" + i + ")重新发送");
            if (i == 0) {
                send1 = 1;
                $('ul>li>p.verification').text("获取邮箱验证码");
                $('ul>li>p.verification').css("background", "#1e9fff");
                clearInterval(iCount);
            }
        }, 1000);
    }
});
//输入密码
$('input[name=pwd]').blur(function () {
    var text = $(this).val();
    var pattern = /^\w{6,20}$/;
    if (!pattern.test(text)) {
        $(this).next().text("！6-20字符以内").css("color", "#fe8d74");
        ok5 = false;
    } else {
        $(this).next().text("√ 输入密码有效").css("color", "#5ce078");
        ok5 = true;
    }
});
//确认密码
$('#again1').blur(function () {
    if (ok5) {
        if ($('#pwd1').val() != $(this).val()) {
            ok6 = false;
            $(this).next().text("两次密码不一致！").css("color", "#fe8d74");
        } else {
            $(this).next().hide();
            ok6 = true;
        }
    }
});
$('#emailnote').blur(function () {
    var key = $(this).val();
    if (MD5(key) == Cookie.Get("key")) {
        $('#emailnote').next().next().text("√ 验证码输入正确").css("color", "#5ce078");
        ok8 = true;
    } else {
        ok8 = false;
        $('#emailnote').next().next().text("验证码错误！").css("color", "#fe8d74");
    }
});

$('button').eq(1).click(function () {
    if (ok4 && ok5 && ok6 && ok8 && account1) {
        new Ajax("/login/update1.jsp").keyvalue($("#emailform").serializeArray()).getJson(function (json) {
            if (json == 1) {
                document.location = "login.jsp";
            } else {

                $('#emailnote').next().next().text("！验证码输入有误").css("color", "#ff5722");
            }
        });
    }
});
$('input[name=account]').blur(function () {
    $('#enterprise').empty();
    var acc = $('input[name=account]').val();
    new Ajax("/api/login/select").keyvalue("account", acc).getJson(function (json) {
        $.each(json, function (k, v) {
            $('#enterprise').append("<option value='" + v["enterprise"] + "'>" + v["enterprise"] + "</option>");
        });
    });
});
$('#account1').blur(function () {
    $('#enterprise1').empty();
    var acc = $('#account1').val();
    new Ajax("/api/login/select").keyvalue("account", acc).getJson(function (json) {
        $.each(json, function (k, v) {
            $('#enterprise1').append("<option value='" + v["enterprise"] + "'>" + v["enterprise"] + "</option>");
        });
    });
})

